package com.hhd.pdf.variable;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

@Getter
@Setter
public class VariablePath {
    private LinkedList<String> path;

    public boolean isSample(VariablePath path) {
        List<String> path1 = path.getPath();
        return isEqual(path1, this.path);
    }

    private boolean isEqual(List<String> path1, List<String> path) {
        if (path1.size() != path.size()) {
            return false;
        }

        for (int i = 0; i < path1.size(); i++) {
            if (!path1.get(i).equals(path.get(i))) {
                return false;
            }
        }
        return true;
    }

    public VariablePath getParent() {
        LinkedList<String> linkedList = new LinkedList<String>(this.path);
        linkedList.removeLast();

        if (linkedList.isEmpty()) {
            return null;
        }

        VariablePath res = new VariablePath();
        res.setPath(linkedList);
        return res;
    }

    public static VariablePath formula(String path) {
        LinkedList<String> tempPath = new LinkedList<>();
        tempPath.add("root");

        String[] split = path.split("\\.");
        tempPath.addAll(Arrays.asList(split));

        VariablePath res = new VariablePath();
        res.setPath(tempPath);
        return res;
    }

    public static VariablePath root() {
        LinkedList<String> tempPath = new LinkedList<>();
        tempPath.add("root");
        VariablePath variablePath = new VariablePath();
        variablePath.setPath(tempPath);
        return variablePath;
    }

    public int getDeep() {
        return path.size();
    }

    public boolean isRoot() {
        if (this.path.size() == 1 && this.path.get(0).equals("root")) {
            return true;
        }
        return false;
    }

    public VariablePath clone() {
        LinkedList<String> tempPath = new LinkedList<>(path);
        VariablePath variablePath = new VariablePath();
        variablePath.setPath(tempPath);
        return variablePath;
    }

    public String fieldName() {
        return this.path.getLast();
    }

    public LinkedList<String> substract(VariablePath path) {
        Iterator<String> iterator1 = this.path.iterator();
        Iterator<String> iterator2 = path.getPath().iterator();
        LinkedList<String> res = new LinkedList<>();

        while (iterator2.hasNext()) {
            if (!iterator1.next().equals(iterator2.next())) {
                throw new RuntimeException("无法相减: " + this.path + " - " + path);
            }
        }
        while (iterator1.hasNext()) {
            res.add(iterator1.next());
        }
        return res;
    }

    public void addPath(String field) {
        this.path.add(field);
    }

    public boolean isBorther(VariablePath variableFormula) {
        return this.getParent().isSample(variableFormula.getParent());
    }

    public boolean isSuper(VariablePath variablePath) {
        Iterator<String> iterator = this.path.iterator();
        Iterator<String> iterator1 = variablePath.getPath().iterator();
        while (iterator.hasNext() && iterator1.hasNext()) {
            if (!iterator.next().equals(iterator1.next())) {
                return false;
            }
        }
        if (!iterator.hasNext() && iterator1.hasNext()) {
            return true;
        }
        return false;
    }
}
